"""
浏览一个需要登录才能看的url
1）请求头从包含cookie信息
    F12--netwok 找到请求头user-agent,cookie
2)cookie参数

"""
import requests

url='https://gitee.com/zhao-qian/PythonCode'
#从浏览器拷贝，构造包含User-Agent.Cookie的请求头
h={
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36',
    'Cookie':'user_locale=zh-CN; oschina_new_user=false; tz=Asia%2FShanghai; sajssdk_2015_cross_new_user=1; Hm_lvt_24f17767262929947cc3631f99bfd274=1605766044; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%228076149%22%2C%22first_id%22%3A%22175df1d588b193-0ee3456b10cdf3-930346c-2073600-175df1d588cc31%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%2C%22%24latest_referrer%22%3A%22%22%7D%2C%22%24device_id%22%3A%22175df1d588b193-0ee3456b10cdf3-930346c-2073600-175df1d588cc31%22%7D; Hm_lpvt_24f17767262929947cc3631f99bfd274=1605766083; gitee-session-n=UUU3bmx0Y0RhYmFsTEJYWlZVd3JJODJUUWtoTER6SGlwUGZOZWlFT3ROdTV6Q201NVpNVTJsTDdnY0x0alQ5NU9LQjhwblNZd2Q3dUNmVXNGbVp1Ylp4TVNZeGpTZnFHVXozN09BdHhSSVp6NjdlQU1lUUFoQUg1amN4c1YvdXVlL0MzSzFRTWh5T3pUL2h2cUxaY0JTUkFDMnRpb1lOeEVDVml3WVRXMXlQS2NMYWh4L2FDdStHMzU0RU9oWWhTRlFkK0JIRzNFemxRQVhlU1ppZVlscGRBTHcvdktLSmNoRVhKdU1vTmhUQm44REdrNzNpdjRYd25UbkpESHV6WlJ0TWZTQkZiMmZJL3hWRGMyUlQ1bXVSYVFnaDhsTlpSOU9vbXZmdjlIZDNVYi9sWGJRM245TXc5a011VzlJU2ZFNkJDZGVyTXQ1bHluc2FJdVU2a0dnMS9BRDdlNFRmWUo2YkVVc0lGcTFvPS0tY09LSE9NeXl5Q0tIUXhnRzRXUXp0dz09--581c2b77e4ac164ec12f6fb51972c03c130a4d30'

}
r=requests.get(url,h)
print(r.content.decode())

#2)cookies参数
h={
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'

}
#cookie字符串，从浏览器拷贝
str_cookies='user_locale=zh-CN; oschina_new_user=false; tz=Asia%2FShanghai; sajssdk_2015_cross_new_user=1; Hm_lvt_24f17767262929947cc3631f99bfd274=1605766044; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%228076149%22%2C%22first_id%22%3A%22175df1d588b193-0ee3456b10cdf3-930346c-2073600-175df1d588cc31%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%2C%22%24latest_referrer%22%3A%22%22%7D%2C%22%24device_id%22%3A%22175df1d588b193-0ee3456b10cdf3-930346c-2073600-175df1d588cc31%22%7D; Hm_lpvt_24f17767262929947cc3631f99bfd274=1605766083; gitee-session-n=UUU3bmx0Y0RhYmFsTEJYWlZVd3JJODJUUWtoTER6SGlwUGZOZWlFT3ROdTV6Q201NVpNVTJsTDdnY0x0alQ5NU9LQjhwblNZd2Q3dUNmVXNGbVp1Ylp4TVNZeGpTZnFHVXozN09BdHhSSVp6NjdlQU1lUUFoQUg1amN4c1YvdXVlL0MzSzFRTWh5T3pUL2h2cUxaY0JTUkFDMnRpb1lOeEVDVml3WVRXMXlQS2NMYWh4L2FDdStHMzU0RU9oWWhTRlFkK0JIRzNFemxRQVhlU1ppZVlscGRBTHcvdktLSmNoRVhKdU1vTmhUQm44REdrNzNpdjRYd25UbkpESHV6WlJ0TWZTQkZiMmZJL3hWRGMyUlQ1bXVSYVFnaDhsTlpSOU9vbXZmdjlIZDNVYi9sWGJRM245TXc5a011VzlJU2ZFNkJDZGVyTXQ1bHluc2FJdVU2a0dnMS9BRDdlNFRmWUo2YkVVc0lGcTFvPS0tY09LSE9NeXl5Q0tIUXhnRzRXUXp0dz09--581c2b77e4ac164ec12f6fb51972c03c130a4d30'
#将cookie字符串构造成字典
dict_cookies={cookie.split('=')[0]:cookie.split('=')[1] for cookie in str_cookies.split(';')}
r=requests.get(url,h,cookies=dict_cookies)
print(r.content.decode())